package cn.fay.chapter14;

import java.util.LinkedHashMap;
import java.util.Map;

public class PetCount extends LinkedHashMap<Class<? extends Pet>, Integer> {
	
		public PetCount(){
			super(MapData.map(LiteralPetCreator.allTypes, 0));
		}
		public void count(Pet pet){
			for (Map.Entry<Class<? extends Pet>,Integer> pair : entrySet()) 
				if (pair.getKey().isInstance(pet)) 
					put(pair.getKey(), pair.getValue()+1);
		}
		public String toString(){
			StringBuilder result=new StringBuilder("{");
			for (Map.Entry<Class<? extends Pet>, Integer> pair : entrySet()) {
				result.append(pair.getKey().getSimpleName());
				result.append("=");
				result.append(pair.getValue());
				result.append(",");
			}
			result.delete(result.length()-1, result.length());
			result.append("}");
			return result.toString();
		}
		public static void main(String[] args) {
			PetCount petCount=new PetCount();
			for (Pet pet:Pets.createArray(120)){
				System.out.println(pet.getClass().getSimpleName()+" ");
				petCount.count(pet);
			}
			System.out.println();
			System.out.println(petCount);
		}
	
}
